package com.mon4cc.simulation.utils;

import com.mon4cc.cmon.adaption.BPMNAdaptor;
import com.mon4cc.simulation.*;
import org.jbpt.pm.ControlFlow;
import org.jbpt.pm.FlowNode;
import org.jbpt.pm.bpmn.Bpmn;
import org.jbpt.pm.bpmn.BpmnControlFlow;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Ping
 * @date 2022/12/19 16:28
 **/
public class BpmnUtils {

    public static Set<FlowNode> nextFlowNodes(Bpmn<BpmnControlFlow<FlowNode>, FlowNode> bpmn, FlowNode node) {
       Collection<ControlFlow<FlowNode>> col = bpmn.getOutgoingControlFlow(node) ;
       if (! ObjectUtils.isEmpty(col)) {
           Set<FlowNode> nextSet  = new HashSet<>() ;
           col.forEach(flow -> {
               nextSet.add(flow.getTarget()) ;}) ;
           return nextSet ;
       }
        return null ;
    }
    public static FlowNodeSimulatedInstance toInstance(FlowNode flowNode, TokenBasedBPMNSimulation bpmnSimulation) {
        if (BPMNAdaptor.isStartEvent(flowNode)) {
            return new StartEventSimulatedInstance(bpmnSimulation) ;
        } else if (BPMNAdaptor.isEndEvent(flowNode)) {
            return new EndEventSimulatedInstance(bpmnSimulation) ;
        } else if (BPMNAdaptor.isActivity(flowNode)) {
            return new ActivitySimulatedInstance(bpmnSimulation, flowNode) ;
        } else if (BPMNAdaptor.isAndGateway(flowNode)) {
            return new AndGatewaySimulatedInstance(bpmnSimulation, flowNode) ;
        } else if (BPMNAdaptor.isXorGateway(flowNode)) {
            return new XorGatewaySimulatedInstance(bpmnSimulation, flowNode) ;
        } else {
            System.err.println("[ERROR] create simulation instance error, since the type of flowNode was out of defined types.") ;
        }
        return null ;
    }
}
